<?php


namespace App\Controller\admin;



use App\Model\admin\SystemAnnex;
use App\Model\admin\SystemAnnexGroup;
use App\Service\AnnexService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Filesystem\FilesystemFactory;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Psr\Http\Message\ResponseInterface;

/**
 * 附件控制器
 * Class AdminController
 * @package App\Controller\admin
 * @Controller(prefix="admin")
 */
class SystemAnnexController extends \App\Controller\AbstractController
{
    /**
     * @Inject
     * @var AnnexService
     */
    protected $annexService;

    /**
     * @Inject
     * @var SystemAnnex
     */
    private $model = null;

    /**
     * @Inject 
     * @var SystemAnnexGroup
     */
    private $groupModel = null;

    /**
     * @RequestMapping(path="annex-list",methods="get")
     */
    public function annexList(): ResponseInterface
    {
        $page = $this->request->input('page',1);
        $limit = $this->request->input('limit',15);
        $group_id = $this->request->input('group_id',0);
        $where = [];
        if (!empty($param['group_id'])){
            $where['group_id'] = $group_id;
        }
        $res['items'] = $this->model->where($where)->forPage($page,$limit)->orderByDesc('id')->get();
        $res['total'] = $this->model->where($where)->count();
        return $this->success($res);
    }

    /**
     * @RequestMapping(path="annex-move",methods="post")
     */
    public function annexMove(): ResponseInterface
    {
        $ids = $this->request->input('ids',1);
        $group_id = $this->request->input('group_id',1);
        $save = $this->model->whereIn('id',$ids)->update([
            'group_id'=>$group_id
        ]);
        return $save ? $this->success() : $this->fail();
    }

    /**
     * @RequestMapping(path="annex-group",methods="get")
     */
    public function annexGroupList(): ResponseInterface
    {
        return $this->success($this->groupModel->get());
    }

    /**
     * @RequestMapping(path="annex-group",methods="post")
     */
    public function annexGroupAdd(): ResponseInterface
    {
        return $this->success($this->groupModel->create(['group_name'=>$this->request->input('group_name','')]));
    }

    /**
     * @RequestMapping(path="annex-group/{id}",methods="delete")
     */
    public function annexGroupDel($id): ResponseInterface
    {
        return $this->success($this->groupModel->where('id',$id)->delete());
    }

    /**
     * 文件上传
     * @RequestMapping(path="annex-upload",methods="post")
     */
    public function example(FilesystemFactory $factory)
    {
        $local = $factory->get('local');
        $file = $this->request->file('file');
        $stream = fopen($file->getRealPath(), 'r+');
        $ext = explode('.',$file->getClientFilename());
        $ext = $ext[count($ext)-1] ?? 'jpg';

        $fileName = md5(uniqid()) . '.' . $ext;

        $local->writeStream(
            'uploads/' . $fileName,
            $stream
        );
        fclose($stream);

        $url = '/uploads/' . $fileName;

        $info = [
            'driver' => 'local',
            'original_name'=> $file->getClientFilename(),
            'file_size'=> $file->getSize(),
            'file_ext'=> $ext,
            'url'=> $url,
            'mime_type'=> $file->getMimeType(),
            'group_id'=> $this->request->input('group_id',0),
            'created_at' => date('Y-m-d H:i:s',time())
        ];

        $save = $this->model->create($info);
        if ($save){
            return $this->success([
                'url' => $url
            ]);
        }
    }
}